package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.user.dto.ApAuthor;
import com.heima.user.dto.AuthDto;
import com.heima.user.dto.WmUser;
import com.heima.user.entity.ApUser;
import com.heima.user.entity.ApUserRealname;
import com.heima.user.feign.ArticleFeign;
import com.heima.user.feign.MediaFeign;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.IApUserRealnameService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.user.service.IApUserService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author Mr_Su
 * @since 2022-06-27
 */
@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements IApUserRealnameService {

    @Autowired
    private IApUserService apUserService;

    @Override
    public ResponseResult listByStatus(AuthDto dto) {
        // 构建分页查询对象
        Page page = new Page(dto.getPage(),dto.getSize());
        // 构建查询条件
        LambdaQueryWrapper<ApUserRealname> lqwApUserRealname = new LambdaQueryWrapper<>();
        // 根据状态进行查询
        if (dto.getStatus() != null) {
            lqwApUserRealname.eq(ApUserRealname::getStatus,dto.getStatus());
        }
        this.page(page,lqwApUserRealname);
        return new PageResponseResult<>(dto.getPage(),dto.getSize(),page.getTotal(),page.getRecords());
    }

    @Override
    @GlobalTransactional
    public ResponseResult auth(AuthDto dto, int type) {
        // type:0 驳回, 1 通过
        ApUserRealname userRealname = this.getById(dto.getId());
        if (type == 0) {
            // 如果是驳回,只需修改实名认证的状态和原因即可

            userRealname.setStatus(2);
            userRealname.setReason(dto.getMsg());
            this.updateById(userRealname);
        }else { // 代表审核通过
            // 查询app用户
            ApUser apUser = apUserService.getById(userRealname.getUserId());

            // 1. 成为自媒体人,远程调用media微服务,保存自媒体人wm_user数据
            WmUser wmUser = saveWmUser(apUser);
            // 2. 成为文章作者,远程调用article微服务,保存自媒体人ap_author数据
            ApAuthor apAuthor = saveApAuthor(apUser,wmUser);
            // 3. 此时已经生成文章作者,可以给wmUser的apAuthorId赋值
            wmUser.setApAuthorId(apAuthor.getId());
            mediaFeign.updateWmUser(wmUser);

            // 4. 修改APP用户的状态
            // 4.1 修改userRealname状态
            userRealname.setStatus(9);
            this.updateById(userRealname);
            // 4.2 修改apUser状态
            apUser.setIsIdentityAuthentication(true); // 已经身份认证
            apUser.setFlag(1); // 自媒体人
            apUserService.updateById(apUser);
        }
        return ResponseResult.okResult();
    }

    @Autowired
    private ArticleFeign articleFeign;

    private ApAuthor saveApAuthor(ApUser apUser,WmUser wmUser) {
        ApAuthor apAuthor = new ApAuthor();
//        apAuthor.setId(); // 主键自增,不用处理
        apAuthor.setName(apUser.getName());
        apAuthor.setType(2); // 0 爬取数据 1 签约合作商 2 平台自媒体人
        apAuthor.setUserId(apUser.getId());
        apAuthor.setWmUserId(wmUser.getId());
        ResponseResult<ApAuthor> responseResult = articleFeign.saveApAuthor(apAuthor);
        if (responseResult.getCode() == 0) {
            return responseResult.getData();
        }else {
            throw new RuntimeException("远程调用失败");
        }

    }

    @Autowired
    private MediaFeign mediaFeign;

    private WmUser saveWmUser(ApUser apUser) {
        WmUser wmUser = new WmUser();
//        wmUser.setId(); // 主键自增,不用处理
        wmUser.setApUserId(apUser.getId());
//        wmUser.setApAuthorId(); // 文章作者id,生成文章作者后再赋值
        wmUser.setName(apUser.getName());
        wmUser.setPassword(apUser.getPassword());
        wmUser.setSalt(apUser.getSalt());
//        wmUser.setNickname();
        wmUser.setImage(apUser.getImage());
//        wmUser.setLocation();
        wmUser.setPhone(apUser.getPhone());
        wmUser.setStatus(apUser.getStatus());
//        wmUser.setEmail();
        wmUser.setType(9); // 9 正常可用
        wmUser.setScore(0);
//        wmUser.setLoginTime();

        ResponseResult<WmUser> responseResult = mediaFeign.saveWmUser(wmUser);
        if (responseResult.getCode() == 0) {
            return responseResult.getData();
        }else {
            throw new RuntimeException("远程调用失败");
        }
    }
}
